<?php
SESSION_START();
header("Content-Type:text/html; charset=utf-8");
require_once("connetMysql.class.php");
$db=new connetMysql;
$con=$db->getConnect();
date_default_timezone_set('prc');

$newsid = $_POST['newsid'];;
$commentusername="";

$comment_content = $_POST['content'];
if(isset($_SESSION['userid'])){
	$commentuserid=$_SESSION['userid'];
	$sqlusername="select * from user where userid='$commentuserid'";
	$resultname = mysqli_query($con,$sqlusername);
	if($row = mysqli_fetch_array($resultname)){
		$commentusername=$row['username'];
	}
	$time = Date("Y-m-d H:i:s",time());
	$sql="INSERT INTO comment (commentnewsid, commentusername, commentcontent,commenttime)  VALUES    ('$newsid','$commentusername','$comment_content','$time')";
	if (!mysqli_query($con,$sql))
  	{
  		die('Error: ' . mysqli_error());
 	}
	$sqlcommentid="select * from comment where commenttime='$time'";
	$resultcommentid = mysqli_query($con,$sqlcommentid);
	if($rowcommentid = mysqli_fetch_array($resultcommentid)){
		$commentid_out=$rowcommentid['commentID'];
	}
	
	$json = array('commentusername'=>  $commentusername,'time' =>  $time ,'commentid' => $commentid_out);
    echo json_encode($json);
}
else{
	$commentusername="网友";
	$time = Date("Y-m-d H:i:s",time());
	$sql="INSERT INTO comment (commentnewsid, commentusername, commentcontent,commenttime)  VALUES    ('$newsid','$commentusername','$comment_content','$time')";
	if (!mysqli_query($con,$sql))
  	{
  		die('Error: ' . mysqli_error());
 	}
	$sqlcommentid="select * from comment where commenttime='$time'";
	$resultcommentid = mysqli_query($con,$sqlcommentid);
	if($rowcommentid = mysqli_fetch_array($resultcommentid)){
		$commentid_out=$rowcommentid['commentID'];
	}
	
	$json = array('commentusername'=>  $commentusername,'time' =>  $time ,'commentid' => $commentid_out);
    echo json_encode($json);
}
?>